package org.alvin.code.tools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字符串工具
 */
public class AStringUtils {

    public static final List<String> NUM_AND_WORD = new ArrayList<>();

    static {
        //解决数字字母
        for (int i = 48; i <= 122; i++) {
            String c = new String(Character.toChars(i));
            if (c.matches("[0-9a-zA-Z]")) {
                NUM_AND_WORD.add(c);
            }
        }
    }

    /**
     * 生成一个固定长度的字符串，包括字母数字
     *
     * @return
     */
    public static String getRandom(int len) {
        List<String> list = new ArrayList<>(NUM_AND_WORD);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        int sub = 0;
        if (len > list.size()) {
            sub = len - list.size();
            len = list.size();
        }
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        int startIndex;
        do {
            startIndex = (int) (Math.random() * list.size());
        } while (startIndex + len > list.size());
        try {
            String result = afterShuffle.substring(startIndex, startIndex + len);
            if (sub > 0) {
                String append = getRandom(sub);
                result += append;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("参数异常");
        }
    }

    /**
     * 替换占位符
     * ? , 3  = ?,?,?
     *
     * @param holder
     * @param join
     * @param size
     * @return
     */
    public static String place(String holder, String join, int size) {
        StringBuilder sb = new StringBuilder();
        if (size <= 0) {
            return "";
        }
        if (holder == null) {
            holder = "";
        }
        if (join == null) {
            join = "";
        }
        for (int i = 0; i < size; i++) {
            sb.append(join).append(holder);
        }
        if (sb.length() > 0 && join.length() > 0) {
            sb.delete(0, join.length());
        }
        return sb.toString();
    }

    public static String place(String holder, int size) {
        return place(holder, "", size);
    }

    /**
     * 统一链接方法
     *
     * @param charts
     * @param delimiter
     * @return
     */
    public static String join(List<String> charts, String delimiter, String prefix, String suffix) {
        if (prefix == null) {
            prefix = "";
        }
        if (suffix == null) {
            suffix = "";
        }
        String finalPrefix = prefix;
        String finalSuffix = suffix;
        return charts.stream().map(item -> finalPrefix.concat(item).concat(finalSuffix)).collect(Collectors.joining(delimiter));
    }

    /**
     * 字符替换
     *
     * @param charts
     * @param delimiter
     * @param prefix
     * @param suffix
     * @return
     */
    public static String join(String[] charts, String delimiter, String prefix, String suffix) {
        return join(Arrays.asList(charts), delimiter, prefix, suffix);
    }

    /**
     * @方法说明: 首字母大写
     */
    public static String firstUpper(String word) {
        return word.substring(0, 1).toUpperCase().concat(word.substring(1));
    }
    /**
     * @方法说明: 首字母大写
     */
    public static String firstLower(String word) {
        return word.substring(0, 1).toLowerCase().concat(word.substring(1));
    }

}
